﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CustomerController.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   MVC Controller to manage customers
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Controllers
{
    #region Using statements

    using System.Linq;
    using System.Web.Mvc;
    using Core.Models;
    using Helpers;

    #endregion

    /// <summary>
    /// MVC Controller to manage customers
    /// </summary>
    public class CustomerController : BaseController
    {
        /// <summary>
        /// Gets the list of customers available in the system.
        /// GET: /Admin/Customer/
        /// </summary>
        /// <returns>Index view</returns>
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Index()
        {
            return View(Entities.Customers);
        }

        /// <summary>
        /// Gets the customer with the given identificator.
        /// GET: /Admin/Customer/Details/5
        /// </summary>
        /// <param name="id">The customer identificator</param>
        /// <returns>Details view</returns>
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Details(int id)
        {
            var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID.Equals(id));
            return View(entity);
        }

        /// <summary>
        /// Gets an empty customer so the user can set all the fields to create a new instance
        /// GET: /Admin/Customer/Create
        /// </summary>
        /// <returns>Create view</returns>
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Create()
        {
            var entity = new Customer();
            return View(entity);
        } 

        /// <summary>
        /// Creates the specified collection.
        /// POST: /Admin/Customer/Create
        /// </summary>
        /// <param name="collection">The form fields collection.</param>
        /// <returns>Index view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Create(FormCollection collection)
        {
            var entity = new Customer();

            try
            {
                UpdateModel(entity);
                Entities.AddObject("Customers", entity);
                Entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }
        
        /// <summary>
        /// Edits the specified id.
        /// GET: /Admin/Customer/Edit/5
        /// </summary>
        /// <param name="id">The customer identificator</param>
        /// <returns>Customer edit view</returns>
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Edit(int id)
        {
            var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID.Equals(id));
            return View(entity);
        }

        /// <summary>
        /// Edits the specified id.
        /// POST: /Admin/Customer/Edit/5
        /// </summary>
        /// <param name="id">The customer identificator</param>
        /// <param name="collection">The form fields collection</param>
        /// <returns>Index view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID.Equals(id));

            try
            {
                UpdateModel(entity);
                Entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }

        /// <summary>
        /// Edits the specified id.
        /// GET: /Admin/Customer/EditCurrent
        /// </summary>
        /// <returns>Customer edit view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult EditCurrent()
        {
            var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID == Customer.CustomerID);
            return View("Edit", entity);
        }

        /// <summary>
        /// Edits the specified id.
        /// POST: /Admin/Customer/EditCurrent
        /// </summary>
        /// <param name="collection">The form fields collection</param>
        /// <returns>Index view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult EditCurrent(FormCollection collection)
        {
            var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID == Customer.CustomerID);

            try
            {
                UpdateModel(entity);
                Entities.SaveChanges();

                return RedirectToAction("Index", "Admin");
            }
            catch
            {
                return View("Edit", entity);
            }
        }

        /// <summary>
        /// Deletes the specified id.
        /// GET: /Admin/Customer/Delete/5
        /// </summary>
        /// <param name="id">The customer identificator</param>
        /// <returns>Delete confirmation view</returns>
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Delete(int id)
        {
            var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID.Equals(id));
            return View(entity);
        }

        /// <summary>
        /// Deletes the specified id.
        /// POST: /admin/Customer/Delete/5
        /// </summary>
        /// <param name="id">The customer identificator</param>
        /// <param name="collection">The form fields collection</param>
        /// <returns>Index view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "superadmin")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var entity = Entities.Customers.FirstOrDefault(c => c.CustomerID.Equals(id));
                Entities.DeleteObject(entity);
                Entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
